<template>
  <div>
<h2 style="margin-top: 0;">销售返利管理</h2>
    <el-row>
      <div class="block">
        <span class="demonstration">选择返利月度</span>
        <el-date-picker v-model="value1" type="month" @change="dateChange" placeholder="选择返利月度">
        </el-date-picker>
      </div>
      <br />
    </el-row>
    <div :class="showView">
    <el-divider></el-divider>
    <el-form ref="form1" :model="form" :inline="true" :rules="rules" style="margin-left: 1.875rem;">
          <h3>厂家返利</h3>
          <el-form-item label="返利项目:" prop="backMonkeyProject">
             <select-tree  :options="factorySelectData" :props="prop" @getValue="getValue1" v-model="this.form.backMonkeyProject" ref="tree" ></select-tree>
           </el-form-item>
            <br />
           <el-form-item label="返利金额:" prop="backMonkeyNumber">
              <el-input-number style="width:100%;" controls-position="right"  v-model="form.backMonkeyNumber" :step="0.1" :min="0" :precision="2"></el-input-number>
            </el-form-item>

            <h3>非厂家返利</h3>
            <el-form-item label="返利项目:" prop="fictoryBackMonkeyProject">
               <select-tree  :options="noFactorySelectData1" :props="prop" @getValue="getValue2" ref="tree1"  v-model="this.form.fictoryBackMonkeyProject"></select-tree>
             </el-form-item>
              <br />
             <el-form-item label="返利金额:" prop="fictoryBackMonkeyNumber">
                <el-input-number style="width:100%;" controls-position="right"  v-model="form.fictoryBackMonkeyNumber" :step="0.1" :min="0" :precision="2"></el-input-number>
              </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-button type="primary" style="float: right;"  @click="save">保存</el-button>
    </div>
  </div>
</template>

<script>
 import selectTree from '../../../components/selectTree.vue'
  export default {
    components: {
      selectTree
    },
     data() {
       return {
         showView:'none', //整体页面是否显示
         showMonth:'',
         clearable:true,
         value1: null, //绑定日期选择器的值（无意义）
         form:{
           backMonkeyNumber:undefined,
           backMonkeyProject:"",
           years: '',
           month: ''
         },
         rules:{},
         prop: {
           value: 'label',
           label: "label",
           children: "children"
         },
         backMonkeyType:[
           {
           label: '厂家',
           value:'厂家'
           },
           {
             value:'非厂家',
           label: '非厂家',
           },
         ],
        noFactorySelectData:[],
        noFactorySelectData1:[
            {
            label: '非厂家返利总收入',
            value: '非厂家返利总收入',
            },
            {
            label: '二手车返利收入',
            value: '二手车返利收入',
            children: [{
              label: '社会交易返利',
              value: '社会交易返利',
            }]
            },
            {
            label: '保险返利收入',
            value: '保险返利收入',
            children: [{
              label: '社会保险返利',
              value: '社会保险返利',
            }]
            },
            {
            label: '精品返利收入',
            value: '精品返利收入',
            children: [{
              label: '供应商返利',
              value: '供应商返利',
            }]
            },
            {
            label: '金融返利收入',
            value: '金融返利收入',
            children: [{
              label: '银行返利',
              value: '银行返利',
            }]
            },
            {
            label: '新车延保返利收入',
            value: '新车延保返利收入',
            children: [{
              label: '供应商返利',
              value: '供应商返利',
            }]
            },
            {
            label: '自店其它佣金收入',
            value: '自店其它佣金收入',
            },
        ],
        factorySelectData: [
          {
          label: '厂家返利总收入',
          value:'厂家返利总收入',
          },
          {
          label: '新车返利收入',
          value:'新车返利收入',
          children: [{
            label: '提车返利',
            value:'提车返利',
          },
          {
            label: '交付返利',
            value:'交付返利',
          }]
          },
          {
          label: '二手车返利收入',
          value:'二手车返利收入',
          children: [{
            label: '厂家交易返利',
            value:'厂家交易返利',
          },
          {
            label: '厂家置换返利',
            value:'厂家置换返利',
          }]
          },
          {
          label: '广宣返利收入',
          value:'广宣返利收入',
          children: [{
            label: '展会返利',
            value:'展会返利',
          },
          {
            label: '线上广宣返利',
            value:'线上广宣返利',
          },
          {
            label: '线下广宣返利',
            value:'线下广宣返利',
          }]
          },
          {
          label: '店面返利收入',
          value:'店面返利收入',
          children: [{
            label: '形象建设返利',
            value:'形象建设返利',
          },
          {
            label: '自营店返利',
            value:'自营店返利',
          }]
          },
          {
          label: '保险返利收入',
          value:'保险返利收入',
          children: [
          {
            label: '厂家返利',
            value:'厂家返利',
          }]
          },
          {
          label: '精品返利收入',
          value:'精品返利收入',
          children: [
          {
            label: '厂家返利',
            value:'厂家返利',
          }]
          },
          {
          label: '金融返利收入',
          value:'金融返利收入',
          children: [
          {
            label: '厂家返利',
            value:'厂家返利',
          }]
          },
          {
          label: '新车延保返利收入',
          value:'新车延保返利收入',
          children: [
          {
            label: '厂家返利',
            value:'厂家返利',
          }]
          },
          {
          label: '厂家其它佣金收入',
          value:'厂家其它佣金收入',
          },
        ]
       }
     },
     methods:{
       save(){
         this.$ajax.post("BackMonkey2/save", this.form).then(res => {
           var result = res.data;
             if (result.success) {
               this.$notify({
                 title: '提示',
                 message: '数据保存成功',
                 type: 'success',
                 position: 'bottom-right'
               });
               this.refresh()
             }
         })
       },
       //刷新方法
       refresh(){
         this.$ajax.post("BackMonkey2/findbydate", {'years':this.form.years,'month': this.form.month}).then(res => {
           var result = res.data
           this.form = result.data
            this.form.id = result.data.backmonkey.id
         })
       },
       dateChange(a){
          this.showView = '';//改变主页面的隐藏样式为空
           this.form.years = (a.getYear() + 1900);//获取年
           this.form.month = (a.getMonth() + 1)//获取月
           this.showMonth = this.form.month;//主页面显示现在是几月（23和72行）
             var flag;
             //通过年和月查找表数据
             this.$ajax.post("BackMonkey/findyears", {'years': this.form.years,'month': this.form.month}).then(res => {
               var result = res.data
               flag = result
               if (flag == true) {
                 this.refresh()
               }else{
                 this.$alert('本月还未录入数据', '提示', {
                   confirmButtonText: '确定',
                   callback: action => {
                 
                   }
                 });
                 emptyObj(this.form)
                 this.emptySelect()
                 this.form.years = (a.getYear() + 1900)
                 this.form.month = (a.getMonth() + 1)
               }
             })

       },

       emptySelect(){
         this.$refs.tree.clearHandle()
          this.$refs.tree1.clearHandle()
       },
       getValue1(a){
         this.form.backMonkeyProject = a
       },
       getValue2(a){
         this.form.fictoryBackMonkeyProject = a
       }
     }
   }
   function emptyObj(obj) {
     if (obj.backMonkeyProject) {
       obj.backMonkeyProject = undefined
     }
     for (var key in obj) {
       var o = obj[key];
       if (o instanceof Object) {
         emptyObj(o);
       } else {
         obj[key] = '';
       }
     }
   }
</script>

<style>
  .none {
    display: none;
  }
</style>
